<template>
  <div>
    <div style="margin-bottom: 12px;display: flex;">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <el-table :data="table_body" border max-height="300">
      <el-table-column prop="rate" label="比率(%)" />
      <el-table-column prop="effectiveFrom" label="生效时间" />
      <el-table-column prop="effectiveTo" label="失效时间" />
      <el-table-column prop="updatedBy" label="最后操作人" />
      <el-table-column prop="updatedAt" label="最后更新时间" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right; padding-top: 10px;">
      <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" @size-change="onPageSizeChange" @current-change="onPageChange" />
    </div>
    <el-dialog :visible.sync="openRateNewModal" width="60%" :close-on-click-modal="false" :append-to-body="true">
      <el-form ref="formData" :model="formData" label-width="150px">
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="生效时间" prop="effectiveFrom">
              <el-date-picker
                v-model="formData.effectiveFrom"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="rate" label="比率">
              <el-input v-model="formData.rate">
                <span slot="append">%</span>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button style="margin-right: 8px" @click="openRateNewModal = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  data() {
    return {
      openRateNewModal: false,
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      table_body: [],
      queryData: {
        surchargeId: '',
        pageSize: 50,
        currentPage: 1
      },
      formDataDefault: {
        id: '',
        effectiveFrom: '',
        rate: ''
      },
      formData: {
        surchargeId: '',
        id: '',
        effectiveFrom: '',
        rate: ''
      }
    }
  },
  computed: {
    pageSize() {
      return this.pageSizeOpts[0]
    }
  },
  created() {
  },
  methods: {
    add() {
      copyProperty(this.formData, this.formDataDefault)
      this.openRateNewModal = true
    },
    show(surchargeId) {
      this.queryData.surchargeId = surchargeId
      this.formData.surchargeId = surchargeId
      this.list()
    },
    list() {
      request({
        url: `/fuel/rate/list`,
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          this.pageTotal = res.data.total
          this.table_body = res.data.records
        }
      })
    },
    edit(row) {
      this.effectiveFrom = ''
      this.formData.id = row.id
      this.formData.rate = row.rate
      this.openRateNewModal = true
    },
    submit() {
      if (this.formData.effectiveFrom) {
        this.formData.id = ''
      }
      request({
        url: 'fuel/rate',
        method: 'post',
        data: this.formData
      }).then(res => {
        if (res.success) {
          this.list()
          this.openRateNewModal = false
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.list()
    }
  }
}
</script>
